//
//  QDQuesTypeTableViewCell.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/3.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 类型图
final class QDQuesTypeCollectionViewCell: NWCollectionViewCell {
    /// 显示类型
    let titleLabel = UILabel()
    /// 选择的图标
    let selectImageView = UIImageView()
    
    override func buildView() {
        super.buildView()
        
        titleLabel.text = "地铁APP"
        titleLabel.textColor = QDColors.black
        titleLabel.font = Font.regular.font(size: 16)
        
        selectImageView.image = "ques_unselect".namedImage
        
        contentView.addSubview(titleLabel)
        contentView.addSubview(selectImageView)
        
        selectImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(18)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(selectImageView.snp.right).offset(4)
            make.height.equalTo(22)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
        
    }
}




/// 问题描述 问题类型
final class QDQuesTypeTableViewCell: NWTableViewCell,QDQuesFeedTableViewCell {
    
    /// 显示标题
    internal let titleLabel = UILabel()
    /// 点击选项
    var didSelectItemHandler: ((String,Int) ->())?
    /// 选择当前的id
    var currentId = "1"
    /// 布局对象
    lazy private var flowLayout: UICollectionViewFlowLayout = {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.sectionInset.left = 20
        flowLayout.sectionInset.right = 20
        flowLayout.sectionInset.bottom = 30
        flowLayout.sectionInset.top = 16
        flowLayout.itemSize = CGSize(width: 68 + 18, height: 22)
        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumLineSpacing = (UIScreen.main.ritl_width - 20 * 2 - (68 + 18) * 3) / 2
        flowLayout.minimumInteritemSpacing = 10
        
        return flowLayout
    }()
    /// 集合视图
    lazy private var collectionView: UICollectionView = {
        return UICollectionView(frame: .zero, collectionViewLayout: self.flowLayout)
    }()
    
    override func buildView() {
        super.buildView()
        titleLabel.text = "请选择问题类型（必选）"
        initTitleLabel()
        makeConstraintsForTitleLabel()
        
        collectionView.backgroundColor = .white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(QDQuesTypeCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom)
        }
    }
}



extension QDQuesTypeTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate {
    
    ///数据源
    private var titles: [String] {
        get {
            return ["地铁APP","导向标识","车站","其他","列车","政策"]
        }
    }
    
    //标题id
    private var titleIds: [String] {
        get {
            return ["1","2","3","0","4","5"]
        }
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        
        if let typeCell = cell as? QDQuesTypeCollectionViewCell {
            
            typeCell.titleLabel.text = titles[indexPath.row]
            
            typeCell.selectImageView.image = (titleIds[indexPath.item] == currentId ?
                "ques_select".namedImage :
                "ques_unselect".namedImage)
        }
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let id = titleIds[indexPath.item]
        if currentId == id { return }
        
        //执行回调
        currentId = id
        //执行回调
        didSelectItemHandler?(currentId,indexPath.item)
        //刷新
        collectionView.reloadData()

    }
}




